const {
  baseUrl,
  remove,
  viewNameFormat,
  sleep,
  nextPlop
} = require("../utils.js");
const path = require("path");
const { layouts } = require("./../choices");
const kebabCase = require("lodash/kebabCase");
const { routeExists, notEmpty } = require("./../validates");
const { refreshViews } = require("./../actions");
module.exports = plop => {
  return {
    description: "创建典型表单页面",
    prompts: [
      {
        type: "list",
        name: "layout",
        message: "输入页面所在布局名称",
        choices: layouts
      },
      {
        type: "input",
        name: "viewName",
        message: "输入页面英文名称,注意不要重名",
        validate: routeExists
      },
      {
        type: "input",
        name: "cname",
        message: "输入页面中文名,注意不要重名",
        validate: notEmpty
      }
    ],
    actions: answers => {
      let { layout, viewName, cname } = answers;
      viewName = viewNameFormat(viewName);
      const viewPath = `src/adminViews/${
        layout ? layout + "/" : ""
      }${viewName}/index.vue`;
      const rulePath = `src/formRules/system/${viewName}Rules.js`;
      remove(path.join(baseUrl, "../", viewName));
      return [
        {
          type: "add",
          path: viewPath,
          templateFile: "templates/formView/view.hbs",
          data: {
            name: viewName,
            idName: kebabCase(viewName),
            cname
          }
        },
        {
          type: "add",
          path: rulePath,
          templateFile: "templates/formView/rule.hbs"
        },
        sleep,
        () => {
          //   nextPlop("router");
        }
      ];
    }
  };
};
